`use strict`

let fs=require('fs');
let path=require('path');
let router=require('koa-router')();

function searchContruller(){
    let files=fs.readdirSync(__dirname);
    let result=files.filter(fileName =>{
        return fileName.endsWith('.js') && fileName !=='index.js';
    })
    return result;
}

function fun(files){
    files.forEach(item=>{
        let fullpath=path.join(__dirname,item);

        let routerobj=require(fullpath);

        for(let item in routerobj){
            let type=routerobj[item][0];
            let fn=routerobj[item][1];
            if(type==='get'){
                router.get(item,fn)
            }else{
                router.post(item,fn)
            };
        }
    })
}

module.exports=function (){
    let files=searchContruller();
    fun(files);
    return router.routes();
}